import React, { PureComponent } from 'react';
import { Form, Carousel, Row, message,Col, List, Avatar, Card, Icon, Divider, Radio, Button } from 'antd';
import { Link, withRouter } from 'react-router-dom';
import axios from 'axios';
import './testcard.css';
const FormItem = Form.Item;

class TestCard extends PureComponent {
  constructor(props) {
    super(props)
    this.state = {
      current: 0,
      card: {
        id: 'id',
        subject_title: 'title',
        answer_a:'a',
        answer_b:'b',
        answer_c:'c',
        answer_d:'d',
      },
      result:[],
      answer:[],
        testResult:''
    }
  }
  componentDidMount() {
    this.getTest();
  }
  getTest = () => {
        axios({
            method:'post',
            url:'http://localhost:8080/subject/getAll',
            data:{token:sessionStorage.getItem("token")}
        }).then((res)=>{
          console.log(res.data.data);
          this.setState({result:res.data.data})
        }).catch((err)=>{console.log(err)});
    }
  submitTest=()=>{
      this.props.form.validateFields((err, values) => {
          if (!err) {
              console.log('Received values of form: ', values);
              const data ={list:[]};
              let str=JSON.stringify(values);
              //getTestAxios
              console.log(str);
              message.success('提交成功');
              // axios({
              //     method:'post',
              //     url:'http://localhost:8080/subject/getAll',
              //     data:{token:sessionStorage.getItem("token"),data}
              // }).then((res)=>{console.log(res.data);this.setState({result:res.data.data})}).catch((err)=>{console.log(err)});
          }else {
              console.log('err');
              console.log(err);
              console.log('err');
          }
          console.log('getTest');
      });
  }
  render() {
    const { getFieldDecorator } = this.props.form;
    const card =this.state.result[this.state.current]||this.state.card;
    console.log(this.state);

    return (
      <div className="action" style={{ margin: '16px auto' }}>
          {
            this.state.result.map((card)=>{
            return (<Card title={card.id+"、 "+card.title} style={{margin:'8px auto'}} key={card.id}>
                {card.result ? <p>{card.result}</p> : <div>
                    <p style={{ textIndent: '2rem' }}>{card.subject_title}</p>
                    <Divider />
                    <div style={{ paddingLeft: '20px' }}>
                        <Card
                            type="inner"
                            bordered={false}
                        >
                            <FormItem>
                                {getFieldDecorator('answer'+card.id, {
                                    rules: [{ required: true, message: '请选择答案' }],
                                })(
                                    <Radio.Group>
                                        <Radio value="a" > {card.a}</Radio>
                                        <Radio value="b" > {card.b}</Radio>
                                        <Radio value="c" > {card.c}</Radio>
                                        <Radio value="d" > {card.d}</Radio>
                                    </Radio.Group>
                                )}
                            </FormItem>
                        </Card>
                    </div>
                </div>
                }
            </Card>);
          })}
    <div style={{textAlign:'center'}}>
        <Button onClick={this.submitTest}>提交</Button>
    </div>

      </div>
    );
  }
}
export default Form.create()(TestCard);